#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
using pii = pair<int, pair<int, int>>;
const int N = 1e5 + 10, INF = 0x3f3f3f3f;

int n, m;
vector<pii> a;
ll prea[N], preb[N];

void solve()
{
    a.clear();
    ll ans = 0;
    cin >> n >> m;
    for (int i = 1;i <= n;i++) {
        int x, y;cin >> x >> y;
        a.push_back({ x - y, { x,y } });
    }
    sort(a.begin(), a.end(), greater());
    for (int i = 0;i < n;i++) {
        prea[i + 1] = prea[i] + a[i].second.first;
        preb[i + 1] = preb[i] + a[i].second.second;
    }

    if (2 * n - 1 <= m) ans = preb[n];
    else ans = 0;

    for (int i = 2;i <= n;i++) {
        if (2 * n - i <= m)
            ans = max(ans, preb[n] - preb[i] + prea[i]);
    }
    cout << ans << '\n';
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}